<?php
/**
* 名称：资讯模块控制器。
* 描述：资讯模块相关的控制响应程式。
*/
final class Controller_News extends Controller_Public_Account{
    /**
    * 描述：列表视图程式。
    * @access protected
    * @return void
    */
    protected function Listview(){
        $data=array(
            'meta'=>array(
                'title'=>'市场资讯',
                'button1_title'=>'发布资讯',
                'button1_link'=>Route::MakeHyperlink(':news::add'),
                'list_title'=>'资讯列表'
            ),
            'base'=>self::$Database->GetLimit(
                'SELECT * FROM '.self::$Database->Table('news').' ORDER BY id DESC',
                Route::GetArguments('page'),
                20
            ),
            'urlArguments'=>array(
                'class'=>'0'
            )
        );
        //~
        foreach($data['base']['Rows'] as &$value){
            $value['class_name']=Model_News::GetClassName($value['class']);
        }
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/news/list.html');
    }

    /**
    * 描述：添加之视图程式。
    * @access protected
    * @return void
    */
    protected function Add(){
        $data=array(
            'meta'=>array(
                'title'=>'发布资讯',
                'action'=>Route::MakeHyperlink(':news::insert')
            ),
            'class'=>$this->GetClass()
        );
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/news/form.html');
    }

    /**
    * 描述：添加之响应程式。
    * @access protected
    * @return void
    */
    protected function Insert(){
        $data=array(
            'title'=>post('title'),'content'=>post('content'),'author'=>post('author'),
            'source'=>post('source'),'label'=>post('label'),'summary'=>post('summary'),
            'datetime'=>post('datetime'),'class'=>post('class'),'cover'=>post('cover'),
            'display'=>post('display')
        );
        //~
        if(empty($data['datetime'])){
            $data['datetime']=now();
        }
        //~
        $this->Feedback(
            self::$Database->Insert(self::$Database->Table('news'),$data),
            array(
                'notice'=>'资讯发布成功，3秒钟后自动返回。',
                'link'=>array(
                    array('name'=>'继续发布','href'=>Route::MakeHyperlink(':news::add')),
                    array('name'=>'前往修改','href'=>Route::MakeHyperlink(':news::edit','id:'.self::$Database->lastInsertId())),
                    array('name'=>'返回列表','href'=>Route::MakeHyperlink(':news::listview'))
                ),
                'autoTurn'=>array(TRUE,3)
            ),
            '发布失败，请稍候再试！'
        );
    }

    /**
    * 描述：修改之视图程式。
    * @access protected
    * @return void
    */
    protected function Edit(){
        $data=array(
            'meta'=>array(
                'title'=>'更新资讯',
                'action'=>Route::MakeHyperlink(':news::update')
            ),
            'base'=>self::$Database->GetRow(
                'SELECT * FROM '.self::$Database->Table('news').' WHERE id=?',
                array(Route::GetArguments('id'))
            ),
            'class'=>$this->GetClass()
        );
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/news/form.html');
    }

    /**
    * 描述：修改之响应程式。
    * @access protected
    * @return void
    */
    protected function Update(){
        $data=array(
            'base'=>array(
                'title'=>post('title'),'content'=>post('content'),'author'=>post('author'),
                'source'=>post('source'),'label'=>post('label'),'summary'=>post('summary'),
                'datetime'=>post('datetime'),'class'=>post('class'),'cover'=>post('cover'),
                'display'=>post('display')
            ),
            'condition'=>array(
                'sql'=>'id=?',
                'parameters'=>array(post('id'))
            )
        );
        //~
        $this->Feedback(
            self::$Database->Update(self::$Database->Table('news'),$data['base'],$data['condition']),
            array(
                'notice'=>'资讯修改成功，3秒钟后自动返回。',
                'link'=>array(
                    array('name'=>'继续修改','href'=>Route::MakeHyperlink(':news::edit','id:'.post('id'))),
                    array('name'=>'返回列表','href'=>Route::MakeHyperlink(':news::listview'))
                ),
                'autoTurn'=>array(TRUE,3)
            ),
            '修改失败，请稍候再试！'
        );
    }

    /**
    * 描述：资讯删除程式之删除。
    * @access protected
    * @return void
    */
    protected function Delete(){
        $id=Route::GetArguments('id');
        //~
        $this->Feedback(
            self::$Database->ExecParameterized(
                'DELETE FROM '.self::$Database->Table('news').' WHERE id=?',array($id)
            ),
            $_SERVER['HTTP_REFERER'],
            '删除失败，请稍候再试！'
        );
    }

    /**
    * 描述：要闻之视图程式。
    * @access protected
    * @return void
    */
    protected function HighlightsEdit(){
        $data=array(
            'meta'=>array(
                'title'=>'资讯头条',
                'action'=>Route::MakeHyperlink(':news::headlineUpdate')
            ),
            'base'=>self::$Database->GetAll(
                'SELECT * FROM '.self::$Database->Table('news_headline').' WHERE id in(1,2) ORDER BY id ASC'
            )
        );
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/news/highlights.html');
    }

    /**
    * 描述：头条之视图程式。
    * @access protected
    * @return void
    */
    protected function HeadlineEdit(){
        $data=array(
            'meta'=>array(
                'title'=>'资讯头条',
                'action'=>Route::MakeHyperlink(':news::headlineUpdate')
            ),
            'base'=>self::$Database->GetAll(
                'SELECT * FROM '.self::$Database->Table('news_headline').' WHERE id in(1,2) ORDER BY id ASC'
            )
        );
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/news/headline.html');
    }

    /**
    * 描述：头条之响应程式。
    * @access protected
    * @return void
    */
    protected function HeadlineUpdate(){
        $data=array(
            'id'=>post('id'),
            'title'=>post('title'),
            'link'=>post('link'),
            'summary'=>post('summary')
        );
        //~
        $execCount=0;
        $dataCount=count($data['id']);
        self::$Database->BeginTransaction();
        for($i=0;$i<$dataCount;$i++){
            $rowData=array(
                'id'=>$data['id'][$i],
                'title'=>$data['title'][$i],
                'link'=>'http://'.str_replace('http://','',$data['link'][$i]),
                'summary'=>$data['summary'][$i]
            );
            $rowCondition=array(
                'sql'=>'id=?',
                'parameters'=>array($data['id'][$i])
            );
            if(self::$Database->Update(self::$Database->Table('news_headline'),$rowData,$rowCondition)){
                $execCount+=1;
            }
        }
        //~
        if($execCount===$dataCount){
            self::$Database->Commit();
            $result=TRUE;
        }
        else{
            self::$Database->RollBack();
            $result=FALSE;
        }
        //~
        $this->Feedback(
            $result,
            array(
                'notice'=>'设置成功，5秒钟后自动返回。',
                'link'=>array(
                    array('name'=>'继续修改','href'=>Route::MakeHyperlink(':news::headlineEdit')),
                    array('name'=>'返回菜单','href'=>Route::MakeHyperlink(':gather'))
                ),
                'autoTurn'=>array(TRUE,5)
            ),
            '修改失败，请稍候再试！'
        );
    }

    /**
    * 描述：获取类别数据。
    * @access private
    * @return void
    */
    private function GetClass(){
        $data=self::$Database->GetAll(
            'SELECT * FROM '.self::$Database->Table('news_class').' WHERE parent is NULL ORDER BY id ASC'
        );
        //~
        foreach($data as &$item){
            $item['data']=self::$Database->GetAll(
                'SELECT * FROM '.self::$Database->Table('news_class').' WHERE parent=? ORDER BY id ASC',
                array($item['id'])
            );
            //~
            foreach($item['data'] as &$subItem){
                $subItem['data']=self::$Database->GetAll(
                    'SELECT * FROM '.self::$Database->Table('news_class').' WHERE parent=? ORDER BY id ASC',
                    array($subItem['id'])
                );
            }
        }
        return $data;
    }
}